package com.cdp.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.SysUserFile;
import com.cdp.common.utils.SecurityUtils;
import com.cdp.system.service.ISysUserFileService;
import com.cdp.system.vo.SysUserFileVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户文件管理接口
 */
@RestController
@RequestMapping("/user/file")
public class SysUserFileController {

    @Autowired
    private ISysUserFileService sysUserFileService;

    /**
     * 查询当前用户的所有文件
     * @return AjaxResult 返回文件列表
     */
    @PostMapping("/list")
    public AjaxResult queryUserFile() {

        Long userId = SecurityUtils.getUserId();
        LambdaQueryWrapper<SysUserFile> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserFile::getUserId,userId);
        wrapper.eq(SysUserFile::getDeleteTime,0);
        List<SysUserFile> list = sysUserFileService.list(wrapper);

        ArrayList<SysUserFileVO> sysUserFileVOS = new ArrayList<>();
        for (SysUserFile userFile : list) {
            SysUserFileVO sysUserFileVO = new SysUserFileVO();
            BeanUtils.copyProperties(userFile,sysUserFileVO);
            sysUserFileVOS.add(sysUserFileVO);
        }


        return AjaxResult.success(sysUserFileVOS);
    }
}
